package com.btc.watcher;

import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Date;

/**
 *  GoxOrder is a representation of a Mt Gox order
 *
 *  @author Dest
 *  @version Apr 20, 2011
 */
public class GoxOrder
{
    private String orderID; //unique ID of the order

    private int orderType; //type of the order
    public static final int TYPE_SELL = 1;
    public static final int TYPE_BUY = 2;

    private int orderStatus; //status of the order
    public static final int STATUS_ACTIVE = 1;
    public static final int STATUS_NOT_ENOUGH_FUNDS = 2;

    private int orderDark; // is the order a dark order?
    public static final int NOT_DARK_ORDER = 0;
    public static final int DARK_AND_NORMAL = 1;
    public static final int DARK_ONLY = 2;

    private double btcAmount; //amount in BTC
    private double orderPrice; //price of each coin
    private String date; // date formatted in Unix epoch time, for example
                         // 4/20 at 14:00 is equivalent to 1303322449



    public GoxOrder(
        String orderID,
        int orderType,
        int orderStatus,
        int orderDark,
        double btcAmount,
        double orderPrice,
        String date)
    {
        super();
        this.orderID = orderID;
        this.orderType = orderType;
        this.orderStatus = orderStatus;
        this.orderDark = orderDark;
        this.btcAmount = btcAmount;
        this.orderPrice = orderPrice;
        this.date = date;
    }

    public GoxOrder()
    {
        super();
        this.orderID = null;
        this.orderType = -1;
        this.orderStatus = -1;
        this.orderDark = -1;
        this.btcAmount = -1;
        this.orderPrice = -1;
        this.date = null;
    }

    /**
     * toString returns all of the details about this object
     */
    @Override
    public String toString() {
        String ret = "Order #"+orderID+"\n";

        if(orderType == TYPE_SELL) {
            ret += "Selling ";
        } else {
            ret += "Buying ";
        }

        ret += btcAmount + " for ";
        ret += "$"+orderPrice;

        if(orderStatus == STATUS_ACTIVE) {
            ret += "\nActive Order, ";
        } else {
            ret += "\nNOT ENOUGH FUNDS, ";
        }

        ret += "Placed "+convertDate();

        if(orderDark == NOT_DARK_ORDER) {
            ret += "\nNot a dark order";
        } else if(orderDark == DARK_ONLY){
            ret += "\n This order is dark ONLY!";
        } else {
            ret += "\n This order is a dark and normal order!";
        }

        return ret;
    }

    /**
     * getSummary returns only a few key details about this object
     * @return String a few key fields from this order
     */
    public String getSummary() {
        String ret = "";

        if(orderType == TYPE_SELL) {
            ret += "Selling ";
        } else {
            ret += "Buying ";
        }

        ret += btcAmount + " BTC for ";
        ret += "$"+orderPrice;

        if(orderStatus == STATUS_ACTIVE) {
            ret += "\nActive Order, ";
        } else {
            ret += "\nNOT ENOUGH FUNDS, ";
        }

        ret += "Placed "+convertDate();

        return ret;
    }

    public String getOrderID()
    {
        return orderID;
    }

    public void setOrderID(String orderID)
    {
        this.orderID = orderID;
    }

    public int getOrderType()
    {
        return orderType;
    }

    public void setOrderType(int orderType)
    {
        this.orderType = orderType;
    }

    public int getOrderStatus()
    {
        return orderStatus;
    }

    public void setOrderStatus(int orderStatus)
    {
        this.orderStatus = orderStatus;
    }

    public int getOrderDark()
    {
        return orderDark;
    }

    public void setOrderDark(int orderDark)
    {
        this.orderDark = orderDark;
    }

    public double getBtcAmount()
    {
        return btcAmount;
    }

    public void setBtcAmount(double btcAmount)
    {
        this.btcAmount = btcAmount;
    }

    public double getOrderPrice()
    {
        return orderPrice;
    }

    public void setOrderPrice(double orderPrice)
    {
        this.orderPrice = orderPrice;
    }

    public String getDate()
    {
        return date;
    }

    public void setDate(String date)
    {
        this.date = date;
    }

    /**
     * convertDate takes our stored date and turns it into a human-readable form
     *
     * @return String the human readable date
     */
    public String convertDate() {

        //This is how to parse a Unix Epoch date
        //We parse the seconds since 1970 * 1000 to give ms since 1970
        Date converted = new Date(Long.parseLong(date) * 1000);

        SimpleDateFormat sdf = new SimpleDateFormat("M/d, h:mm a", Locale.US);
        String ret = sdf.format(converted);
        return ret;
    }


}
